limsSample.sampleTypeStaticsPanel = {

    option: {
        /*title: {
            text: 'ORDER SAMPLE TREND CHART',
        },*/
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: [Gikam.propI18N('batch'), Gikam.propI18N('sample')]
        },
        toolbox: {
            show: true,
        },
        calculable: true,
        xAxis: [
            {
                type: 'category',
                //data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
                data: [Gikam.propI18N('Jan'), Gikam.propI18N('Feb'), Gikam.propI18N('Mar'), Gikam.propI18N('Apr'),
                    Gikam.propI18N('May'),
                    Gikam.propI18N('Jun'), Gikam.propI18N('Jul'), Gikam.propI18N('Aug'), Gikam.propI18N('Sep'),
                    Gikam.propI18N('Oct'), Gikam.propI18N('Nov'), Gikam.propI18N('Dec')]
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],

    },


    colors: [],

    setEchartsParam: function (option) {
        var _this = this;
        return {
            type: 'echarts',
            id: 'sample-statics-echarts',
            option: option
        }
    },

    isExistItem: function (arr, item, key) {
        for (var idx = 0; idx < arr.length; idx++) {
            if (item[key] == arr[idx]) {
                return true;
            }
        }
        return false;
    },

    newItem: function (item) {
        return {
            month: item.month,
            count: item.count
        };
    },
    getGroup: function (data, key) {
        let groups = {};
        data.forEach(c => {
            let value = c[key];
            groups[value] = groups[value] || [];
            groups[value].push(c);
        });
        return groups;
    },

    buildPanelOption: function (year) {
        var _this = this;
        if (Gikam.isEmpty(year)) {
            year = new Date().getFullYear();
        }
        let dataUrl = Gikam.printf('/secure/baseline/samplemanage/samples/selectBatchSampleStatics/{year}/count', {
            year: year
        })
        Gikam.postSync(dataUrl, Gikam.getJsonWrapper(null, [null, []])).done(function (data) {
            //data=JSON.parse(data)
            // console.log(data);
            if (Gikam.isNotEmpty(data)) {
                var dataArr = [];
                var data2 = _this.getGroup(data, 'month');
                const map = {
                    "01": "Jan",
                    "02": "Feb",
                    "03": "Mar",
                    "04": "Apr",
                    "05": "May",
                    "06": "Jun",
                    "07": "Jul",
                    "08": "Aug",
                    "09": "Sep",
                    "10": "Oct",
                    "11": "Nov",
                    "12": "Dec",
                }

                var xKey = Object.keys(data2);
                var names = data2[xKey[0]].map(item => {
                    return item.type;
                })
                xKey = xKey.map(item => {
                    return map[item]
                })
                _this.option.xAxis.data = xKey;
                var dataArr = [];
                names.forEach((item, index) => {
                    var obj
                    if (item === 'sample') {
                        obj = {
                            color: 'red',
                            label: {
                                show: true,
                                position: 'top'
                            },
                            name: Gikam.propI18N('sample'),
                            type: 'bar',
                            data: []
                        };
                    }
                    if (item === 'batch') {
                        obj = {
                            color: 'orange',
                            label: {
                                show: true,
                                position: 'top'
                            },
                            name: Gikam.propI18N('batch'),
                            type: 'bar',
                            data: []
                        };
                    }

                    // var key=Object.keys(data2);
                    let months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
                    for (let i = 0; i < 12; i++) {
                        let month = months[i]
                        const dataObj = data2[month].find(subItems => {
                            return subItems.type === item
                        })
                        obj.data.push(dataObj.count)
                    }
                    /*				    key.forEach(subItem=>{
                                          const dataObj=data2[subItem].find(subItems=>{
                                                return subItems.type==item
                                            })
                                            obj.data.push(dataObj.count)
                                        })*/

                    dataArr.push(obj)
                })
                // console.log(dataArr)
                _this.option.series = dataArr;
                // console.log('series:'+_this.option.series)
            } else {
                _this.option.series = [];
            }


            /*Gikam.create('echarts', {
                id : 'sample-statics-edit-list',
                renderTo : document.body,
                config : {
                    search: {
                        type: 'form',
                        id: 'sample-statics-select-form',
                        fields: [{
                            title: '年份选择',
                            field: 'year',
                            type: 'year',
                            onChange: function (field, value) {
                                if(Gikam.isNotEmpty(value)){
                                    _this.buildPanelOption(value);
                                }
                            }
                        }]
                    },
                },
                option : _this.option,
            });*/
        });
        // return _this.option;
    },

    getCodeCount: function (data, months) {
        var counts = [];
        for (let i = 0; i < months.length; i++) {
            for (let j = 0; j < data.length; j++) {
                if (months[i] === data[j].month) {
                    counts[i] = data[j].count;
                    break;
                }
            }
            if (Gikam.isEmpty(counts[i])) {
                counts[i] = 0;
            }
        }
        return counts;
        /*	if (data) {
                data.forEach(item => {
                    counts.push(item.count);
                });
                return counts;
            }*/
    },

    getEchartsItemColor: function () {
        var colors;
        var url = IFM_CONTEXT + '/secure/core/module/sys/codes/queries/raw';
        Gikam.postSync(url, Gikam.getJsonWrapper({
            f: {
                codeCategoryId_EQ: 'color'
            },
        })).done(function (data) {
            if (data && !Gikam.isEmpty(data.rows)) {
                colors = data.rows.map(function (item) {
                    return item.code;
                })
            }
        });
        return colors;
    },

    create: function () {
        var window = Gikam.create('window', {
            renderTo: document.body
        });
        var _this = this;
        _this.colors = _this.getEchartsItemColor();
        _this.buildPanelOption();
        Gikam.create('layout', {
            renderTo: window.$dom,
            id: 'sample-statics-edit-list',
            center: {
                height: 400,
                items: [{
                    type: 'echarts',
                    id: 'sample-statics-edit-list-echarts',
                    option: _this.option,
                    config: {
                        search: {
                            type: 'form',
                            id: 'sample-statics-select-form',
                            fields: [{
                                title: 'year selection',
                                field: 'year',
                                type: 'year',
                                value: new Date().getFullYear(),
                                onChange: function (field, value) {
                                    if (Gikam.isNotEmpty(value)) {
                                        _this.buildPanelOption(value);
                                        // console.log(_this.option);
                                        var echart = Gikam.getComp('sample-statics-edit-list-echarts');
                                        echart.setOption(_this.option, true);
                                        // console.log(_this.option);
                                        //console.log(echart.option);

                                    }
                                }
                            }]
                        },
                    },

                }]
            }
        })
        /*Gikam.create('echarts', {
            id : 'sample-statics-edit-list',
            renderTo : document.body,
            config : {
                search: {
                    type: 'form',
                    id: 'sample-statics-select-form',
                    fields: [{
                        title: '年份选择',
                        field: 'year',
                        type: 'year',
                        onChange: function (field, value) {
                            if(Gikam.isNotEmpty(value)){
                                _this.buildPanelOption(value);
                            }
                        }
                    }]
                },
            },
            option : _this.option,
        });*/
    },

    init: function (param) {
        this.param = param;
        this.create();
    }
}